<template>
	<div>
		<el-table :stripe="true" :border="true" :data="stuList.filter(i => i.stuname)" style="width: 100%">
			<el-table-column align="center" label="学号" prop="stuno"> </el-table-column>
			<el-table-column align="center" label="姓名" prop="stuname"> </el-table-column>
			<el-table-column align="center" label="专业" prop="stumajor"> </el-table-column>
			<el-table-column align="center" label="性别" prop="stugender"> </el-table-column>
			<el-table-column align="center" label="年级" prop="stugrade"> </el-table-column>
			<el-table-column align="center" label="电话" prop="stuphone"> </el-table-column>
			<el-table-column align="center" label="居住地" prop="stuaddess"> </el-table-column>
			<el-table-column align="center" label="户籍" prop="stunative"> </el-table-column>
			<el-table-column align="center">
				<template slot="header">
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</template>
				<template slot-scope="scope">
					<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          
					<el-popconfirm @confirm="handleDelete(scope.$index, scope.row)" confirm-button-text="确认" cancel-button-text="不用了" icon="el-icon-info" icon-color="red" title="确定删除吗？">
						<el-button slot="reference" size="mini" type="danger">删除</el-button>
					</el-popconfirm>
				</template>
			</el-table-column>
		</el-table>

		<update-dialog :newStudent="sToEdit" :editId="editId" @refresh="$emit('refresh')" :visible.sync="dialogVisible" :type="dialogType"></update-dialog>
	</div>
</template>
<script>
import Cookie from 'js-cookie'
import axios from 'axios'
import { Message } from "element-ui";
import UpdateDialog from "@/components/UpdateDialog";
export default {
	name: "StuTable",
	props: ["stuList"],
	components: {
		UpdateDialog,
	},
	data() {
		return {
			dialogVisible: false,
      		dialogType: "add",
			editId: null,
			sToEdit: {},
		};
	},
	watch: {},
	mounted() {},
	methods: {
		handleDelete(key, row) {
			let token = Cookie.get("token");
				axios
					.post("http://114.67.241.121:8088/stu/del",null, {
						headers: {
							Authorization: token,
						},
            params: {
              stuid: row.stuid,
            },
					})
					.then(res => {
						Message.success(res.data.msg);
            // 触发刷新
            this.$emit('refresh')
					});
		},
		handleEdit(key, row) {
      		this.dialogType="edit"
			this.editId=row.stuid
			this.sToEdit={...row}
			this.dialogVisible = true;

		},
		handleAdd() {
      		this.dialogType="add"
			this.dialogVisible = true;
		},
	},
};
</script>
<style lang="less" scoped>
* {
	font-size: 1.2rem;
}
</style>
